package cn.edu.ctbu.api01.service;

import cn.edu.ctbu.api01.domain.Student;
import cn.edu.ctbu.api01.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class StudentService {

    @Autowired
    StudentRepository studentRepository;

    public List<Student> getAll() {
        return studentRepository.findAll();
    }

    public Student getById(Integer id) {
        return studentRepository.findById(id).orElse(null);
    }

    public void save(Student student) {
        studentRepository.save(student);
    }

    public Student update(Student student) {
        return studentRepository.save(student);
    }

    public Student insert(Student student) {
        return studentRepository.save(student);
    }

    public void delete(Integer id) {
        studentRepository.deleteById(id);
    }

    public List<Student> findByName(String name) {
        return studentRepository.findByNameLike("%" + name + "%");
    }

    public List<Student> findBySex(Integer sex) {
        return studentRepository.findBySex(sex);
    }


}
